#include<vector>
#include<queue>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>>res;
        if (root == NULL) return res;
        queue<TreeNode*> queue_;
        queue_.push(root);
        while (!queue_.empty()) {
            int size = queue_.size();
            vector<int>tmp(size);
            for (int i = 0; i < size; ++i) {
                TreeNode* node = queue_.front();
                queue_.pop();
                tmp[i] = node->val;
                if (node->left != NULL)
                    queue_.push(node->left);
                if (node->right != NULL)
                    queue_.push(node->right);
            }
            res.push_back(std::move(tmp));
        }
        return res;
    }
};